 <template>
  <view class="container">
  <!-- 备选框 -->
    <view class="default">
      <view
        class="answers"
        v-for="(answer, index) in defaultAnswers"
        :key="index"
        @touchstart="choose(answer)"
      >{{ answer }}</view>
    </view>

   
<!-- input输入框 -->
  <uni-card>
    <!-- <uni-easyinput
      type="textarea"   // uniapp 里面没有给主键盘遮住输入框的解决方案
                            改组件作废 替换为van field
      placeholder="请输入描述"
      :inputBorder="false"
      @input="input"
      class="inner-input"
      v-model="words"
      :clearable="false"
    /> -->
  <van-cell-group>
    <van-field
      :value="words"
      size="large"
      type="textarea"
      placeholder="请根据题目输入内容"
      :autosize="{minHeight:100,maxHeight:200}"
      :border="false"
      :cursor-spacing="60"
      @input="input"
    /> 
  </van-cell-group> 


    <van-row gutter="10">
      <van-col span="16">
        <van-button
        icon="service"
        round
        block
        color="linear-gradient(to right, #4bb0ff, #6149f6)"
        @touchstart="speak()"
        @touchend="finishSpeak()"
        >
          按住进行语音输入
        </van-button>
      </van-col >
      <van-col span="8">
        <van-button
        icon="delete-o"
        type="primary"
        plain
        round
        block
        color=" #b5aba7"
        @touchstart="clear()">
        清空
        </van-button>
      </van-col>
    </van-row>

     <!-- <uni-grid :column="2" :showBorder="false">
      <uni-grid-item class="inner-voice" @mousedown="speak()">
        <view class="my-grid-item" @click="(e) => e.preventDefault()" @mousedown="(e) => test(e)">
          <view
            class="button-to-speak"
            :style="{
              backgroundColor: !pressDown ? '#339acc' : 'white',
            }"
            @touchstart="speak()"
            @touchend="finishSpeak()"
          >
            <uni-icons type="mic" size="50" :color="pressDown ? '#339acc' : 'white'" />
          </view>
        </view>
      </uni-grid-item>
      <uni-grid-item class="inner-voice">
        <view class="my-grid-item">
          <view class="button-to-confirm" @touchstart="clear()">
            <uni-icons type="reload" size="50" color="#339acc" />
          </view>
        </view>
      </uni-grid-item>
    </uni-grid> -->
  
  </uni-card>


  </view>
</template>

<script>
import { iat } from "../../utils";
export default {
  props: ["defaultAnswers"],
  data() {
    return {
      //二次重构更新内容
      //
      pressDown: false,
      words: "",
      task: undefined,
      recorderManager: undefined,
      sendQueue: [],
      intervalId: 0,
      isFirst: false
    };
  },
  mounted() {
    // iat
    this.recorderManager = uni.getRecorderManager();
    this.recorderManager.onError(err => console.log(err));
    this.intervalId = setInterval(() => {
      if (this.sendQueue.length === 0)
        return;
      console.log(this.sendQueue.length)
      const data = this.sendQueue[0];
      this.sendQueue.splice(0, 1);

      console.log(data.frameBuffer);
      iat.upstream(this.task, data.frameBuffer, this.isFirst ? 0 : (data.isLastFrame ? 2 : 1),
        res => {
          const resdata = JSON.parse(res.data).data;
          console.log(resdata)
          console.log(data.isLastFrame)
          if (data.isLastFrame)
            for (const words of resdata.result.ws) {
              this.words += words.cw[0].w;
            }
        },
        err => console.log(err)
      );
      if (this.isFirst)
        this.isFirst = false;
    });

  },
  methods: {
    speak() {
      const info = iat.generateAuthoriztion();
      this.pressDown = true;
      this.task = uni.connectSocket({
        url: encodeURI(`wss://${info.host}/v2/iat?authorization=${info.authorization}&date=${info.date}&host=${info.host}`),
        success: () => {
        },
        fail: err => console.log(err)
      });
      this.recorderManager.start({
        format: "PCM",
        frameSize: 1.25,
        numberOfChannels: 1
      });
      uni.showToast({
        title: "请开始说话"
      });
      this.isFirst = true;
      this.recorderManager.onFrameRecorded(data => {
        this.sendQueue.push(data);
      });
    },
    finishSpeak() {
      this.pressDown = false;
      this.recorderManager.stop();
    },
    input(e) {
      this.words =e.detail
      console.log(this.words)
      this.$emit("input", e)
    },
    clear() {
      console.log("before:",this.words)
      this.words = "";
       console.log("after",this.words)
    },
    choose(e) {
      console.log("before:",this.words)
      this.words += e
      console.log("after",this.words)
    }
  },
  watch: {
    words() {
      this.$emit("input", this.words);
    }
  }
};
</script>

<style scoped>
.container {
  /* padding: 20rpx; */
}
.inner-input {
  height: 100%;
  font-size: large;

  padding: 20rpx;
}
.inner-input {
}
.inner-voice {
  position: relative;
}
  /* height: 80%;
} */
.button-to-confirm {
  display: flex;
  align-items: center;
  justify-content: center;
}
.my-grid-item {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;

  width: 100%;
  height: 100%;
}
.speak {
  display: flex;
}

.default {
  display: flex;

  justify-content: center;


  flex-direction: row;
  flex-wrap: wrap;
}

.answers {
  min-width: 150rpx;
  border-radius: 5rpx;
  border: 1rpx solid rgb(177, 177, 177);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 5rpx;
  margin: 5rpx;
}
</style>
